<?php

namespace app\admin\model;

use EasyWeChat\Factory;
use think\Model;

class AuthGroup extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    public static $app;

    public function getNameAttr($value, $data)
    {
        return __($value);
    }

    /**
     * 初始化
     */
    public function initialize()
    {
        self::$app = Factory::work(Admin::config());
    }

    /**
     * 添加标签
     * @param $tagName
     * @param $tagId
     * @return mixed
     */
    public static function addTab($tagName,$tagId)
    {
        $data = self::$app->tag->create($tagName, $tagId);
        return $data;
    }

    /**
     * 修改标签
     * @param $tagName
     * @param $tagId
     * @return mixed
     */
    public static function updateTab($tagName,$tagId)
    {
        $data = self::$app->tag->update($tagId, $tagName);
        return $data;
    }

    /**
     * 删除标签
     * @param $tagId
     * @return mixed
     */
    public static function deleteTab($tagId)
    {
        $data = self::$app->tag->delete($tagId);
        return $data;
    }


    /**
     * 获取列表
     * @return mixed
     */
    public static function listTab()
    {
        return  self::$app->tag->list();
    }

    /**
     * 获取部门下面的成员
     * @param $tagId
     * @return mixed
     */
    public static function getTab($tagId)
    {
        return self::$app->tag->get($tagId);
    }

    /**
     * 更新标签成员
     * @param $tagId
     * @param $data
     * @return mixed
     */
    public static function updateTabUser($tagId,$data)
    {
        return self::$app->tag->tagUsers($tagId,$data);
    }

    /**
     * 删除标签成员
     * @param $tagId
     * @param $data
     * @return mixed
     */
    public static function delTabUser($tagId, $data)
    {
        return self::$app->tag->untagUsers($tagId, $data);
    }

    /**
     * 获取所有的标签id   销毁管理员ID
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAllTab()
    {
        $data =  collection(AuthGroup::field('group_concat(id) as ids')->order('weigh', 'desc')->select())->toArray();
        $data = explode(',',$data[0]['ids']);
        unset($data[0]);
        return $data;
    }
}
